<?php
#error_reporting(E_ALL ^ E_NOTICE);
if($_SERVER['REMOTE_ADDR'] == '127.0.0.1') {
	error_reporting(24567);
}

include("mainfile.php");

$page = new Page;

$id = $_GET['id']*1;
echo $page->printPage($id);

class Page {

    // page data
    var $id = 0; // ID of the selected page
	var $dir      = 0; // directory ID of the selected page
	var $name = "";
	var $language = "";
	var $created = "";
	var $title = "";
	var $keywords = "";
	var $content = "";
	
	
    function printPage($id = 1) {
	    global $tpl,$common;
	    
		$this->getPageData($id);
		
        // get the template
        $tpl->getTpl();
        $tpl->replaceCommonTags();
        $tpl->replaceTags(array(
        TITLE       =>  $this->title,
        KEYWORDS    =>  $this->keywords,
        CONTENT     =>  $tpl->parse($this->content),
		NAV_TOP     =>  $common->navMenuTop(),
        NAVMENU     =>  $this->navMenu(),
		BOTTOM      =>  $common->Bottom($this->id),
		PAGEROUTE   =>  $common->getRoute($this->dir)
        ));
        
		return $tpl->page;
	}

    function getPageData($id = 1) {
        global $table,$db,$lang,$common;
	    
		if ($id == 0 || !$this->validPage($id, $lang->current)) { $id = $common->getSmallestId($lang->current); }
		
    	$result = $db->query("SELECT * FROM $table->page WHERE id='$id' AND language='$lang->current'");
	    if ($db->numRows($result) == 0)
    	{
	        $this->content = _NOCONTENTAVAILABLE;
    	}
	    else
    	{
	        $row = $db->nextRecord($result);
			$this->id       = $row->id;
			$this->dir      = $row->dir;
			$this->language = $row->language;
	        $this->title    = $row->title;
    		$this->keywords = $row->keywords;
	    	$this->content  = $row->content;
	    }
    }


	function validPage ($id, $language) {
	    global $table,$db;
		
		if ($db->numRows($db->query("SELECT * FROM $table->page WHERE id='$id' AND language='$language'")) == 0)
		    { return false; }
		else
		{
		    return true;
		}
	}



    function navMenu() {
return;
	    global $table,$db,$html,$common;
		

		$result = $db->query("SELECT * FROM $table->dir WHERE parent='$this->dir' AND language='$this->language' ORDER BY id");

		    while ($row = $db->nextRecord($result))
    		{
			    if ($row->id == $this->id) { $bgcolor = "#ffa000"; } else { $bgcolor = "#006595"; }
				$str.= "<br>";
	    	    $str.= $html->table(
				    $html->tr(
					    $html->td(
						    "<a href='".GLOBAL_SELF."?id=".$common->getSmallestId($this->language,$row->id)."'>".
						    $row->name.
							"</a>"
						,0,"","navmenu_white")
					)
				,"100%",0,3,0,$bgcolor,"navmenu");
		    }
		
	    
		$result = $db->query("SELECT * FROM $table->page WHERE dir='$this->dir' AND language='$this->language' ORDER BY id");
		    while ($row = $db->nextRecord($result))
    		{
			    if ($row->id == $this->id) { $bgcolor = "#badeef"; } else { $bgcolor = ""; }
				$str.= "<br>";
	    	    $str.= $html->table(
				    $html->tr(
					    $html->td(
						    "<a href='".GLOBAL_SELF."?id=$row->id'>".
						    $row->name.
							"</a>"
						,0,"","navmenu")
					)
				,"100%",0,3,0,$bgcolor,"navmenu");
		    }
		
		return $str;
	}


}
?>